#@head() 
#@header()
<title>APP版本更新</title>
#@aside("marketing")
<div class="content-wrapper">
	<section class="content">
		<div class="nav-tabs-custom">
            <ul class="nav nav-tabs">
			  <li class="active"><a href="#(path)/admin/page/marketing-applist">APP版本更新</a></li>
            </ul>
            <div class="tab-content">
              <div class="tab-pane active" id="tab_1">
				<div class="box-body">
					<table id="example1" style="width:800px;" class="table table-striped">
						<tbody>
							<tr>
								<th align="right" style="width:100px;">操作系统：</th>
								<td>
									<select id="system" class="form-control">
										<option value="android" >安卓</option>
										<option value="ios" >苹果</option>
									 </select>
								</td>
							</tr>
							<tr>
								<th align="right" style="width:100px;">版本名称：</th>
								<td><input id="name" type="text" class="form-control" placeholder="版本名称"></td>
							</tr>
							<tr>
								<th align="right" style="width:100px;">描述信息：</th>
								<td><input id="summary" type="text" class="form-control" placeholder="描述信息"></td>
							</tr>
							<tr>
								<th align="right" style="width:100px;">版本号：</th>
								<td><input id="code" type="text" class="form-control" placeholder="版本号"></td>
							</tr>
							<tr>
								<th align="right" style="width:100px;">类型：</th>
								<td>
									<select id="type" class="form-control">
										<option value="official">正式版</option>
									 </select>
								</td>
							</tr>
							<tr>
								<th align="right" style="width:100px;">强制升级：</th>
								<td>
									<select id="isForce" class="form-control">
										<option value="1" >是</option>
										<option value="0" >否</option>
									 </select>
								</td>
							</tr>

							<tr id="uploadurl">
								<th align="right" style="width:100px;">上传路径：</th>
								<td><input id="url" type="text" class="form-control" placeholder="上传路径"></td>
							</tr>
							<tr id="upload">
								<th align="right" style="width:100px;">文件上传：</th>
								<td>
								<div id="uploader" class="wu-example">
									<!--用来存放文件信息-->
									<div id="thelist" class="uploader-list"></div>
									<div class="btns">
										<div id="picker">选择文件</div>
									</div>
								</div>
								</td>
							</tr>
						</tbody>
					</table>
				</div>
				<div class="box-footer">
					<button id="submit_addcarousel" type="submit" style="margin-left:50px;" class="btn btn-info ">提交</button>
					<button onclick="window.location.href='#(path)/admin/page/marketing-appcarousel'" type="submit" style="margin-left:10px;" class="btn  ">取消</button>
				  </div>
              </div>
            </div> 
            <!-- /.tab-content -->
          </div>
		  
	</section>
</div>
#@footer()
<script>

$("#system").change(function(){
	if($("#system").val()=="ios"){$("#upload").hide();$("#uploadurl").hide();}
	if($("#system").val()=="android"){$("#upload").show();$("#uploadurl").show();}
});
var src = '';
$(document).ready(function(){
	var id = GetRequest().id;
	if(!$.isBlank(id)){
	// 初始化Web Uploader
		$.getJSON("/app/versionInfo?id="+id, function(userinfo) {
			console.log(userinfo);
			$("#system").val(userinfo.data.system);
			if(userinfo.data.system=="ios"){$("#upload").hide();}
			$("#name").val(userinfo.data.name);
			$("#summary").val(userinfo.data.summary);
			$("#code").val(userinfo.data.code);
			$("#isForce").val(userinfo.data.is_force);
			$("#type").val(userinfo.data.type);
			$("#url").val(userinfo.data.url);
		});
	}else{
		id="";
	}
	$("#submit_addcarousel").click(function() {
		layer.confirm('是否确认提交？', {btn : [ '确认', '取消' ]}, function() {
			var params = getFormVals("#tab_1");
			params['url'] = $("#url").val(); 
			ajax("#(path)/app/setVersion?token=#(session.token??)&id="+id,params,function(data){
				if(data.code=='success'){
					layer.msg(data.msg, {icon:1},function(){
						location.href="#(path)/admin/page/marketing-applist"
					});
				}else if(data.code=='error'){
					layer.msg(data.msg, {icon : 2});
				}
			});
		}, function() {
			layer.msg('已取消！', {icon : 1});
		});
	}); 
	
	var uploader = WebUploader.create({
		// swf文件路径
		swf: '#(path)/assets/plugins/webuploader/Uploader.swf',

		auto: true,

		// 文件接收服务端。
		server: '#(path)/sys/uploadFile',

		// 选择文件的按钮。可选。
		// 内部根据当前运行是创建，可能是input元素，也可能是flash.
		pick: '#picker',

		// 不压缩image, 默认如果是jpeg，文件上传前会压缩一把再上传！
		resize: false
	});
	// 当有文件被添加进队列的时候
	uploader.on( 'fileQueued', function( file ) {
		$("#thelist").empty();
		$("#thelist").append( '<div id="' + file.id + '" class="item">' +
			'<h4 class="info">' + file.name + '</h4>' +
			'<p class="state">等待上传...</p>' +
		'</div>' );
	});
	// 文件上传过程中创建进度条实时显示。
	uploader.on( 'uploadProgress', function( file, percentage ) {
		var $li = $( '#'+file.id ),$percent = $li.find('.progress .progress-bar');

		// 避免重复创建
		if ( !$percent.length ) {
			$percent = $('<div class="progress progress-striped active">' +
			  '<div class="progress-bar" role="progressbar" style="width: 0%">' +
			  '</div>' +
			'</div>').appendTo( $li ).find('.progress-bar');
		}

		$li.find('p.state').text('上传中');
		$percent.css( 'width', percentage * 100 + '%' );
	});
	uploader.on( 'uploadSuccess', function(file,response) {
		 
		$('#'+file.id ).find('p.state').text('已上传');		
		src = "/attachment"+response['url'];
		$("#url").val(src);
	});

	uploader.on( 'uploadError', function( file ) {
		$( '#'+file.id ).find('p.state').text('上传出错');
	});

	uploader.on( 'uploadComplete', function( file ) {
		$( '#'+file.id ).find('.progress').fadeOut();
	});
	
	function GetRequest() {  
	   var url = location.search; //获取url中"?"符后的字串  
	   var theRequest = new Object();  
	   if (url.indexOf("?") != -1) {  
		  var str = url.substr(1);  
		  strs = str.split("&");  
		  for(var i = 0; i < strs.length; i ++) {  
			 theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);  
		  }  
	   }  
	   return theRequest;  
	}  
});

</script>
</body>
</html>

